#include "spriteref.h"

#include "ImageManager.h"

using namespace sf;

SpriteRef::SpriteRef() : Sprite(), m_imageRef((ImageRef*)0)
{

}

SpriteRef::SpriteRef(const QString& imagePath, const QPoint& position, const QRect& subRect)
    : Sprite()
{
    setImage(imagePath);
    setPosition(position);
    setSubRect(subRect);
}

const QPixmap& SpriteRef::getPixmap() const
{
    return m_imageRef->pixmap;
}

const QPoint& SpriteRef::getPosition() const
{
    return m_position;
}

const QRect& SpriteRef::getSubRect() const
{
    return m_subRect;
}

void SpriteRef::setImage(const QString& imagePath)
{
    m_imageRef = ImageManager::getInstance().getImageRef(imagePath);
    SetImage(m_imageRef->image);
}

void SpriteRef::setPosition(const QPoint& position)
{
    //sets the QPixmap position
    m_position = position;

    //sets the sf::Sprite position
    SetPosition(position.x(), position.y());
}

void SpriteRef::setSubRect(const QRect& subRect)
{
    if(!subRect.isNull())
    {
        //if the given rect is valid, sets the subRect as it is given
        SetSubRect(IntRect(subRect.left(), subRect.top(), subRect.right()+1, subRect.bottom()+1));
        m_subRect = subRect;
    }
    else
    {
        //if the given rect is null, sets the subRect as the entire image rect
        SetSubRect(IntRect(0, 0, m_imageRef->image.GetWidth(), m_imageRef->image.GetHeight()));
        m_subRect = QRect(m_imageRef->pixmap.rect());
    }
}

void SpriteRef::move(int dx, int dy)
{
    //"moves" the QPixmap
    m_position.rx() += dx;
    m_position.ry() += dy;

    //moves the sf::Sprite
    Move(dx, dy);
}
